<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <link rel="icon" href="./images/gongzhenglogo.png">
    <link rel="stylesheet" href="./bootstrap-5.3.0-alpha1-dist/css/bootstrap.min.css">
    <style>
        body {
            background: linear-gradient(135deg, #e0e7ff 0%, #f8fafc 100%);
        }
        .profile-container {
            max-width: 900px;
            margin: 48px auto;
            background: rgba(255,255,255,0.98);
            border-radius: 22px;
            box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.18);
            padding: 40px 36px 32px 36px;
            transition: box-shadow 0.3s;
        }
        .profile-header {
            border-bottom: 2px solid #e0e7ff;
            margin-bottom: 32px;
            padding-bottom: 18px;
        }
        .profile-header h2 {
            font-size: 2.2rem;
            font-weight: 700;
            color: #3b3b5c;
            letter-spacing: 2px;
        }
        .profile-info {
            display: flex;
            align-items: center;
            gap: 40px;
        }
        .profile-avatar {
            width: 110px;
            height: 110px;
            border-radius: 50%;
            background: #f3f3f3 url('./images/avatar.png') center/cover no-repeat;
            border: 4px solid #e0e7ff;
            box-shadow: 0 2px 12px 0 rgba(31, 38, 135, 0.08);
        }
        .profile-details {
            flex: 1;
            font-size: 1.15rem;
            color: #444;
            line-height: 2.1;
        }
        .profile-details strong {
            color: #3b3b5c;
            font-weight: 600;
        }
        .orders-title {
            font-size: 1.3rem;
            font-weight: 700;
            margin: 32px 0 18px 0;
            color: #3b3b5c;
            letter-spacing: 1px;
        }
        table.table {
            background: #f9fafb;
            border-radius: 14px;
            overflow: hidden;
            box-shadow: 0 2px 8px 0 rgba(31, 38, 135, 0.06);
        }
        table.table thead tr {
            background: linear-gradient(90deg, #6366f1 0%, #60a5fa 100%);
            color: #fff;
            font-size: 1.08rem;
        }
        table.table tbody tr {
            transition: background 0.2s;
        }
        table.table tbody tr:hover {
            background: #e0e7ff;
        }
        table.table td, table.table th {
            vertical-align: middle;
            text-align: center;
        }
        @media (max-width: 700px) {
            .profile-container { padding: 16px 4px; }
            .profile-info { flex-direction: column; gap: 16px; }
            .profile-avatar { width: 70px; height: 70px; }
        }
    </style>
</head>
<body>
    <!-- 登录模态框 -->
    <div class="modal fade" id="loginModal" tabindex="-1" aria-labelledby="loginModalLabel" aria-hidden="true" data-bs-backdrop="static">
      <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="loginModalLabel">账户登录</h5>
          </div>
          <div class="modal-body">
            <form id="loginForm">
              <div class="mb-3">
                <label for="companyId" class="form-label">公司编号（秘钥）</label>
                <input type="text" class="form-control" id="companyId" required>
              </div>
              <button type="submit" class="btn btn-primary w-100">登录</button>
            </form>
          </div>
        </div>
      </div>
    </div>

    <div class="profile-container" id="mainContent" style="display:none;">
        <div class="profile-header">
            <h2>企业信息</h2>
        </div>
        <div class="profile-info mb-4">
            <div class="profile-avatar"></div>
            <div class="profile-details">
                <div><strong>公司名称：</strong><span id="user-company">XX有限公司</span></div>
                <div><strong>公司编号：</strong><span id="user-id">xx</span></div>
                <div><strong>法人：</strong><span id="user-name">张三</span></div>
                <div><strong>邮箱：</strong><span id="user-email">zhangsan@example.com</span></div>
                <div><strong>手机号：</strong><span id="user-phone">13800000000</span></div>
                <div><strong>公司注册时间：</strong><span id="user-join">2024-05-01</span></div>
            </div>
        </div>
        <div class="orders-title">企业订单</div>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>订单号</th>
                    <th>订单类型</th>
                    <th>商品名称</th>
                    <th>下单时间</th>
                    <th>金额</th>
                    <th>状态</th>
                </tr>
            </thead>
            <tbody id="orders-list">
                <tr>
                    <td>20240501001</td>
                    <td>招标或者投标</td>
                    <td>水泥</td>
                    <td>2024-05-01</td>
                    <td>¥1000</td>
                    <td>已完成</td>
                </tr>

            </tbody>
        </table>
    </div>
    <script src="./bootstrap-5.3.0-alpha1-dist/js/bootstrap.bundle.min.js"></script>
    <script>
      document.addEventListener('DOMContentLoaded', function() {
        var loginModal = new bootstrap.Modal(document.getElementById('loginModal'), {backdrop: 'static', keyboard: false});
        loginModal.show();

        // 新增：AI回答和思考区域
        const aiAnswerDiv = document.createElement('div');
        aiAnswerDiv.id = 'aiAnswer';
        aiAnswerDiv.style.margin = '24px 0 12px 0';
        aiAnswerDiv.innerHTML = '<h4>执剑评语</h4><div id="aiAnswerContent" style="background:#f9f9f9;padding:12px;border-radius:8px;"></div>';
        document.getElementById('mainContent').appendChild(aiAnswerDiv);

        const aiThinkDiv = document.createElement('div');
        aiThinkDiv.id = 'aiThink';
        aiThinkDiv.style.margin = '12px 0 24px 0';
        aiThinkDiv.innerHTML = '<h4>执剑深度思考</h4><div id="aiThinkContent" style="background:#f5f5f5;padding:12px;border-radius:8px;"></div>';
        document.getElementById('mainContent').appendChild(aiThinkDiv);

        document.getElementById('loginForm').onsubmit = function(e) {
          e.preventDefault();
          var companyId = document.getElementById('companyId').value.trim();
          if(companyId) {
            var url = 'http://localhost:8080/api/company-detail/byCompanyId?companyId=' + encodeURIComponent(companyId);
            fetch(url)
              .then(response => response.json())
              .then(data => {
                if(data.companyInfo){
                  document.getElementById('user-company').textContent = data.companyInfo.companyName || '';
                  document.getElementById('user-id').textContent = data.companyInfo.companyId || '';
                  document.getElementById('user-name').textContent = data.companyInfo.legalPerson || '';
                  document.getElementById('user-email').textContent = data.companyInfo.email || '';
                  document.getElementById('user-phone').textContent = data.companyInfo.phone || '';
                  document.getElementById('user-join').textContent = data.companyInfo.registrationDate || '';
                  // 渲染订单
                  var ordersList = document.getElementById('orders-list');
                  ordersList.innerHTML = '';
                  let orderSummary = '';
                  if(Array.isArray(data.orders)){
                    data.orders.forEach(function(order){
                      var dateStr = order.orderTime ? order.orderTime.split('T')[0] : '';
                      var tr = document.createElement('tr');
                      tr.setAttribute('data-order', JSON.stringify({
                        orderId: order.orderId,
                        orderType: order.orderType,
                        productName: order.productName,
                        orderTime: dateStr,
                        amount: order.amount,
                        status: order.status
                      }));
                      tr.style.cursor = 'pointer';
                      tr.innerHTML =
                        '<td>' + (order.orderId || '') + '</td>' +
                        '<td>' + (order.orderType || '') + '</td>' +
                        '<td>' + (order.productName || '') + '</td>' +
                        '<td>' + dateStr + '</td>' +
                        '<td>¥' + (order.amount != null ? order.amount.toFixed(2) : '') + '</td>' +
                        '<td>' + (order.status || '') + '</td>';
                      ordersList.appendChild(tr);
                    });
                  }
                  loginModal.hide();
                  document.getElementById('mainContent').style.display = '';

                  // 拼接总结文本
                  let summary = `总结：公司名称${data.companyInfo.companyName}，公司编号${data.companyInfo.companyId}，法人${data.companyInfo.legalPerson}，邮箱${data.companyInfo.email}，手机号${data.companyInfo.phone}，注册时间${data.companyInfo.registrationDate}。${orderSummary}`;

                  // 调用AI接口
                  fetch('http://localhost:8080/ai/chat?prompt=' + encodeURIComponent(summary)+"给我一段商业化的总结")
                    .then(resp => resp.text())
                    .then(text => {
                      // 解析AI返回内容
                      let answer = text;
                      let think = '';
                      const thinkMatch = text.match(/<think>([\s\S]*?)<\/think>/);
                      if(thinkMatch){
                        think = thinkMatch[1];
                        answer = text.replace(/<think>[\s\S]*?<\/think>/, '');
                      }
                      document.getElementById('aiAnswerContent').textContent = answer.trim();
                      document.getElementById('aiThinkContent').textContent = think.trim();
                    })
                    .catch(() => {
                      document.getElementById('aiAnswerContent').textContent = 'AI接口请求失败';
                      document.getElementById('aiThinkContent').textContent = '';
                    });
                } else {
                  alert('查询失败，请检查公司编号是否正确');
                }
              })
              .catch(() => {
                alert('查询失败，请检查公司编号是否正确');
              });
          } else {
            alert('请输入公司编号');
          }
        };
    // 订单点击跳转合同界面
    document.getElementById('orders-list').addEventListener('click', function(e) {
      var tr = e.target.closest('tr');
      if(tr && tr.hasAttribute('data-order')) {
        var orderInfo = tr.getAttribute('data-order');
        // 获取企业信息
        var companyInfo = {
          companyName: document.getElementById('user-company').textContent,
          companyId: document.getElementById('user-id').textContent,
          legalPerson: document.getElementById('user-name').textContent,
          email: document.getElementById('user-email').textContent,
          phone: document.getElementById('user-phone').textContent,
          registrationDate: document.getElementById('user-join').textContent
        };
        // 合并企业信息和订单信息
        var contractData = {
          order: JSON.parse(orderInfo),
          company: companyInfo
        };
        sessionStorage.setItem('selectedOrder', JSON.stringify(contractData));
        window.location.href = 'contract.html';
      }
    });
  });
    </script>
</body>
<!-- 你可以在这里用AJAX请求用户信息和订单数据并渲染到页面 -->
</html>